package product;
import java.io.File;
import java.io.FileWriter;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )throws Exception{
        String path = "E:\\WORK\\project"; // 指定目录
        String project = "onelinespace";
        String companyName = "onelinespace"; // 公司名称
        /// String [] modules = {"common","model","dao","web","server","api"};
        Module [] modules = {
                new Module("common",false,"jar"),
                new Module("model",false,"jar"),
                new Module("dao",true,"jar"),
                new Module("service",false,"jar"),
                // new Module("controller",false,"jar"),
                new Module("api",false,"jar"),
                new Module("web",true,"war"),
        };
        App app = new App();
        app.build(path,project,companyName,modules);
    }

    private void build(String path,String project,String companyName,Module [] modules)throws Exception{
        String rootDir = path + File.separator + project;
        File rootFile = new File(rootDir);
        rootFile.mkdirs();
        String rootPom = rootDir + File.separator+ "pom.xml";
        File pomFile = new File(rootPom);
        pomFile.createNewFile();
        this.rootPomTxt(pomFile,project,companyName,modules);
        for (Module item : modules){
            this.module(rootDir,project,companyName,item);
        }
    }

    private void module(String root,String project,String companyName,Module info) throws Exception{
        String moduleDir = root + File.separator + project +"-"+info.getName();
        File moduleFile = new File(moduleDir);
        moduleFile.mkdirs();
        String modulePom = moduleDir + File.separator +"pom.xml";
        File modulePomFile = new File(modulePom);
        modulePomFile.createNewFile();

        this.modulPomTxt(modulePomFile,project,companyName,info);

        String src = moduleDir + File.separator +"src"+File.separator+"main"+File.separator+"java"+File.separator+"com"
                +File.separator+companyName+File.separator+info.getName();
        File srcFile = new File(src);
        srcFile.mkdirs();
        if(info.isResources()) {
            String resourcePath = moduleDir + File.separator + "src" + File.separator + "main" + File.separator + "resources";
            File resourcePathFile = new File(resourcePath);
            resourcePathFile.mkdirs();
        }
    }


    private void modulPomTxt(File file,String project,String companyName,Module info) throws Exception {
        String pomTxt = "<?xml version=\"1.0\"?>\n" +
                "<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\n" +
                "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
                "  <modelVersion>4.0.0</modelVersion>\n" +
                "\n" +
                "  <parent>\n" +
                "    <groupId>com."+companyName+"</groupId>\n" +
                "    <artifactId>"+project+"</artifactId>\n" +
                "    <version>0.0.1-SNAPSHOT</version>\n" +
                "  </parent>\n" +
                "\n" +
                "    <artifactId>"+project+"-"+info.getName()+"</artifactId>\n" +
                "    <packaging>"+info.getPackaging()+"</packaging>\n" +
                "    <version>0.0.1-SNAPSHOT</version>\n" +
                "\n" +
                "  <name>"+project+"-"+info.getName()+"</name>\n" +
                "  <url>http://maven.apache.org</url>\n" +
                "\n" +
                "  <properties>\n" +
                "    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>\n" +
                "    <java.version>1.8</java.version>\n" +
                "  </properties>\n" +
                "\n" +
                "  <dependencies>\n" +
                "  \t<dependency>\n" +
                "  \t\t<groupId>com.alibaba</groupId>\n" +
                "\t\t<artifactId>fastjson</artifactId>\n" +
                "  \t</dependency>\n" +
                "\t<dependency>\n" +
                "\t    <groupId>org.slf4j</groupId>\n" +
                "\t    <artifactId>slf4j-api</artifactId>\n" +
                "\t</dependency>\n" +
                "\t<dependency>\n" +
                "\t\t<groupId>org.apache.commons</groupId>\n" +
                "\t    <artifactId>commons-lang3</artifactId>\n" +
                "\t</dependency>\n" +
                "  </dependencies>\n" +
                "</project>";
        FileWriter fw = new FileWriter(file);
        fw.append(pomTxt.toString());
        fw.close();
    }

    private void rootPomTxt(File file,String project,String companyName,Module [] modules) throws Exception{
        StringBuilder rootTxt = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" +
                "  <modelVersion>4.0.0</modelVersion>\n" +
                "\n" +
                "  <parent>\n" +
                "    <groupId>org.springframework.boot</groupId>\n" +
                "    <artifactId>spring-boot-starter-parent</artifactId>\n" +
                "    <version>1.5.4.RELEASE</version>\n" +
                "    <relativePath/>\n" +
                "  </parent>\n" +
                "\n" +
                "  <groupId>com."+companyName+"</groupId>\n" +
                "  <artifactId>"+project+"</artifactId>\n" +
                "  <version>0.0.1-SNAPSHOT</version>\n" +
                "  <packaging>pom</packaging>\n" +
                "\n" +
                "  <name>"+project+"</name>\n" +
                "  <url>http://maven.apache.org</url>\n" +
                "\n" );
        rootTxt.append("\n    <modules>\n");
        for(Module item : modules){
            rootTxt.append("      <module>" + project + "-"+item.getName()+"</module>\n");
        }
        rootTxt.append("    </modules>\n");
        rootTxt.append("\n" +
                "\n" +
                "  <properties>\n" +
                "    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>\n" +
                "    <java.version>1.8</java.version>\n" +
                "  </properties>\n" +
                "\n" +
                "  <dependencyManagement>\n" +
                "  \t<dependencies>\n");

        for(Module item : modules){
            rootTxt.append("\t\t<dependency>\n"+
                    "\t\t    <groupId>com."+project+"</groupId>\n" +
                    "\t\t    <artifactId>"+project+"-"+item.getName()+"</artifactId>\n" +
                    "\t\t    <version>0.0.1-SNAPSHOT</version>\n" +
                    "\t\t</dependency>\n");
        }

        rootTxt.append("\t\t<dependency>\n" +
                "\t\t    <groupId>com.alibaba</groupId>\n" +
                "\t\t    <artifactId>fastjson</artifactId>\n" +
                "\t\t    <version>1.2.34</version>\n" +
                "\t\t</dependency>\n" +
                "\t\t<dependency>\n" +
                "\t\t    <groupId>com.alibaba</groupId>\n" +
                "\t\t    <artifactId>druid</artifactId>\n" +
                "\t\t    <version>1.1.0</version>\n" +
                "\t\t</dependency>\n" +
                "\t\t<dependency>\n" +
                "\t\t    <groupId>mysql</groupId>\n" +
                "\t\t    <artifactId>mysql-connector-java</artifactId>\n" +
                "\t\t    <version>5.1.42</version>\n" +
                "\t\t</dependency>\n" +
                "\t\t<dependency>\n" +
                "\t\t    <groupId>org.mybatis</groupId>\n" +
                "\t\t    <artifactId>mybatis</artifactId>\n" +
                "\t\t    <version>3.4.4</version>\n" +
                "\t\t</dependency>\n" +
                "        <dependency>\n" +
                "            <groupId>org.mybatis.spring.boot</groupId>\n" +
                "            <artifactId>mybatis-spring-boot-starter</artifactId>\n" +
                "            <version>1.1.1</version>\n" +
                "        </dependency>\n" +
                "\t\t<dependency>\n" +
                "            <groupId>org.apache.commons</groupId>\n" +
                "            <artifactId>commons-lang3</artifactId>\n" +
                "            <version>3.5</version>\n" +
                "        </dependency>\n" +
                "        <dependency>\n" +
                "            <groupId>com.github.pagehelper</groupId>\n" +
                "            <artifactId>pagehelper-spring-boot-starter</artifactId>\n" +
                "            <version>1.1.2</version>\n" +
                "        </dependency>\n" +
                "        <dependency>\n" +
                "            <groupId>org.springframework.boot</groupId>\n" +
                "            <artifactId>spring-boot-devtools</artifactId>\n" +
                "            <optional>true</optional>\n" +
                "        </dependency>\n" +
                "    </dependencies>\n" +
                "  </dependencyManagement>\n" +
                "\n" +
                "  <dependencies>\n" +
                "      <dependency>\n" +
                "          <groupId>org.springframework.boot</groupId>\n" +
                "          <artifactId>spring-boot-starter-web</artifactId>\n" +
                "      </dependency>\n" +
                "\n" +
                "      <dependency>\n" +
                "          <groupId>org.springframework.boot</groupId>\n" +
                "          <artifactId>spring-boot-starter-tomcat</artifactId>\n" +
                "          <scope>provided</scope>\n" +
                "      </dependency>\n" +
                "\n" +
                "      <dependency>\n" +
                "          <groupId>com.alibaba</groupId>\n" +
                "          <artifactId>fastjson</artifactId>\n" +
                "      </dependency>\n" +
                "      <dependency>\n" +
                "          <groupId>org.apache.commons</groupId>\n" +
                "          <artifactId>commons-lang3</artifactId>\n" +
                "      </dependency>\n" +
                "      <dependency>\n" +
                "          <groupId>org.springframework.boot</groupId>\n" +
                "          <artifactId>spring-boot-starter-tomcat</artifactId>\n" +
                "          <scope>provided</scope>\n" +
                "      </dependency>\n" +
                "\n" +
                "  </dependencies>\n" +
                "\n" +
                "  <profiles>\n" +
                "  \t<profile>\n" +
                "         <id>local</id>\n" +
                "         <activation>\n" +
                "             <activeByDefault>true</activeByDefault>\n" +
                "         </activation>\n" +
                "         <properties>\n" +
                "             <profileActive>local</profileActive>\n" +
                "         </properties>\n" +
                "     </profile>\n" +
                "     <profile>\n" +
                "            <id>prev</id>\n" +
                "            <activation>\n" +
                "                <activeByDefault>false</activeByDefault>\n" +
                "            </activation>\n" +
                "            <properties>\n" +
                "                <profileActive>prev</profileActive>\n" +
                "            </properties>\n" +
                "        </profile>\n" +
                "     <profile>\n" +
                "            <id>prod</id>\n" +
                "            <activation>\n" +
                "                <activeByDefault>false</activeByDefault>\n" +
                "            </activation>\n" +
                "            <properties>\n" +
                "                <profileActive>prod</profileActive>\n" +
                "            </properties>\n" +
                "        </profile>\n" +
                "\n" +
                "  </profiles>\n" +
                "\n" +
                "\n" +
                "  <build>\n" +
                "      <resources>\n" +
                "          <resource>\n" +
                "              <directory>src/main/resources</directory>\n" +
                "              <filtering>true</filtering>\n" +
                "          </resource>\n" +
                "      </resources>\n" +
                "  \t<plugins>\n" +
                "  \t\t<plugin>\n" +
                "  \t\t\t<groupId>org.apache.maven.plugins</groupId>\n" +
                "  \t\t\t<artifactId>maven-compiler-plugin</artifactId>\n" +
                "  \t\t\t<configuration>\n" +
                "  \t\t\t\t<source>1.8</source>\n" +
                "  \t\t\t\t<target>1.8</target>\n" +
                "                <encoding>UTF-8</encoding>\n" +
                "  \t\t\t</configuration>\n" +
                "  \t\t</plugin>\n" +
                "        <plugin>\n" +
                "            <groupId>org.apache.maven.plugins</groupId>\n" +
                "            <artifactId>maven-resources-plugin</artifactId>\n" +
                "            <version>2.5</version>\n" +
                "        </plugin>\n" +
                "  \t</plugins>\n" +
                "  </build>\n" +
                "\n" +
                "</project>");

        FileWriter fw = new FileWriter(file);
        fw.append(rootTxt.toString());
        fw.close();
    }

    private static class Module{
        Module(String name,boolean resources,String packaging){
            this.name = name;
            this.resources = resources;
            this.packaging = packaging;
        }
        private String name;
        private boolean resources;
        private String packaging;

        public String getName() {
            return name;
        }

        public boolean isResources() {
            return resources;
        }

        public String getPackaging() {
            return packaging;
        }
    }
}
